package cn.neu.leon.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.neu.leon.util.Percentage;
import cn.neu.leon.util.Sentiment;
import cn.neu.leon.util.WeiboCrawler;

public class WeeklyColumn extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter pw = response.getWriter();
		String url = request.getParameter("userUrl");
		// String url = "http://weibo.cn/2765365605/profile?vt=4";
		Map<String, double[]> perMap = new LinkedHashMap<String, double[]>();
		WeiboCrawler wc;
		try {
			wc = new WeiboCrawler(url);
			if(!wc.isIdExist())
			{
				pw.print("不存在该用户");
				pw.close();
			}
			else
			{
				ArrayList<String> userInfo = wc.getUserInfo();
				System.out.println(userInfo);
				perMap = wc.getPerMap();
				
				String time; //状态发布时间
				double[] per = new double[7]; //七种状态情感值
				int n;   //一个星期中的第几天
				double week[][] = new double[7][7];  //7行7列的二维数组，一个星期7天中，每天7种情感值
				
				JSONObject data = new JSONObject();
				JSONArray series = new JSONArray();
				
				//遍历情感map
				Set<Map.Entry<String, double[]>> entrySet = perMap.entrySet();
				for (Map.Entry<String, double[]> entry : entrySet) {
	                time = entry.getKey();
	                per = entry.getValue();
					System.out.println(time + "," + per);
					
					DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					Date date = df.parse(time);
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);
					n = cal.get(Calendar.DAY_OF_WEEK)-1;  //星期几，其中周天为0
					for(int i=0;i<7;i++)
					{
						week[n][i] += per[i];
					}
				}
				
				//每一天的情感值，按照比例格式化
				for(int i = 0;i<7;i++)
				{
					week[i] = Percentage.format(week[i]);
				}
				
				for (int i = 0; i < 7; i++) {
					JSONObject jo = new JSONObject();
					JSONArray  ja =new JSONArray();
					jo.put("name", Sentiment.sentimentCN[i]);
					for(int j=0;j<7;j++)
					{
						ja.add(week[j][i]);
					}
					jo.put("data", ja);
					series.add(jo);
				}

				data.put("userInfo", userInfo);
				data.put("series", series);

				String str = data.toString();
				System.out.println(str);
				
				pw.print(str);
				pw.close();
			}
			
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
